animeRequests.ts ➔ removeUnknownUsers   B
last analyzed

Complexity

Conditions 4

Size

Total Lines 48
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 37
dl 0
loc 48
rs 8.9919
c 0
b 0
f 0
1
import { getGlobalConfiguration, SETTINGS_requestBeautifyPage } from '../configuration/configuration';
2
import * as core from '../utils/aniwatchCore';
3
import * as color from '../utils/colors';
4
5
export function init(): void {
6
    getGlobalConfiguration().getProperty(SETTINGS_requestBeautifyPage, value => {
7
        if (value) {
8
            core.registerScript((node: Node) => {
9
                // run the scripts
10
                if (node instanceof HTMLElement) {
11
                    changeFollowedStarColor(node);
12
                    changeBorderColorOwnRequests(node);
13
                    removeUnknownUsers(node);
14
                }
15
            }, "/requests");
16
        }
17
    });
18
}
19
20
function changeFollowedStarColor(node: HTMLElement): void {
21
    const STAR_ICON = 'star';
22
23
    // find stars
24
    let followedItems = Array.from(node.querySelectorAll('i')).filter(i => i.innerText.trim() === STAR_ICON);
25
26
    // change color
27
    followedItems.forEach(item => item.style.color = color.aniBlue);
28
}
29
30
function changeBorderColorOwnRequests(node: HTMLElement): void {
31
    const TARGET_TAG_NAME = 'MD-LIST-ITEM'; // tagName is upper case
32
33
    let updateFunc = (item: HTMLElement): void => {
34
        let profileLink = item.querySelectorAll('a[href*="/profile/"]:not([href="/profile/false"])');
35
36
        // highlight left border for own request
37
        if (profileLink.length > 0) {
38
            item.style.borderLeftColor = color.aniBlue
39
        }
40
    }
41
42
    // are we target tag?
43
    if (node.tagName === TARGET_TAG_NAME) {
44
        updateFunc(node);
45
    } else {
46
        // find items -> all 
47
        let requestItems = node.querySelectorAll('md-list-item');
48
49
        // update borders
50
        requestItems.forEach(item => {
51
            if (item instanceof HTMLElement) {
52
                updateFunc(item);
53
            }
54
        });
55
    }
56
}
57
58
function removeUnknownUsers(node: HTMLElement): void {
59
    const TARGET_TAG_NAME = 'MD-LIST-ITEM'; // tagName is upper case
60
61
    let updateFunc = (item: Element) => {
62
        // find user profile link -> own request
63
        let profileLink = item.querySelectorAll('a[href*="/profile/"]:not([href="/profile/false"])');
64
65
        // find divs
66
        let upperDiv = node.querySelector('[layout-align="start center"][flex]')
67
        let lowerDiv = upperDiv.parentElement.nextElementSibling;
68
69
        // remember Data
70
        let anime = lowerDiv.textContent;
71
        let profileData = upperDiv.innerHTML;
72
73
        // add user note if own request
74
        if (profileLink.length > 0) {
75
            // Workaround to avoid innerHTML because of #38, see https://devtidbits.com/2017/12/06/quick-fix-the-unsafe_var_assignment-warning-in-javascript/
76
            let parser = new DOMParser();
77
            let parsedDocument = parser.parseFromString(profileData, 'text/html');
78
79
            lowerDiv.innerHTML = '';
80
            while (parsedDocument.body.hasChildNodes()) {
81
                lowerDiv.appendChild(parsedDocument.body.removeChild(parsedDocument.body.firstChild));
82
            }
83
        }
84
        // remove if foreign request.
85
        else {
86
            lowerDiv.innerHTML = ' ';
87
        }
88
89
        // exchange data
90
        let bElement = document.createElement('b');
91
        bElement.textContent = anime;
92
        upperDiv.innerHTML = ``;
93
        upperDiv.appendChild(bElement);
94
    }
95
96
    if (node.tagName === TARGET_TAG_NAME) {
97
        updateFunc(node);
98
    } else {
99
        // find items -> all 
100
        let requestItems = node.querySelectorAll('md-list-item');
101
102
        // change border color if profile link is not 'false'
103
        requestItems.forEach(item => {
104
            updateFunc(item);
105
        });
106
    }
107
}